package com.b1t.springcloud.service;


import com.b1t.springcloud.dao.AccountDao;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 账户业务实现类
 */
@Service
@Slf4j
public class AccountServiceImpl implements AccountService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AccountServiceImpl.class);


    @Resource
    AccountDao accountDao;

    /**
     * 扣减账户余额
     */
    @Override
    public void decrease(Long userId, BigDecimal money) {
        LOGGER.info("------->account-service中扣减账户余额开始");
        //模拟超时异常，全局事务回滚
        //暂停几秒钟线程
//        Integer number =0;
//        Random rand = new Random();
//        for (int i = 0; i < 1; i++) {
//            number=rand.nextInt(8) + 1;
//        }
//        log.info("数值:{}",number);
//        if(number>=4){
        try {
            TimeUnit.SECONDS.sleep(30);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }

//        }
        accountDao.decrease(userId,money);
        LOGGER.info("------->account-service中扣减账户余额结束");
    }


}




